/*

ORBITER MODULE: BulletBase implementation 

Project: surface-physics : http://code.google.com/p/surface-physics/

Description :This file implements the BulletBase VESSEL3 derived class. Other
vessels can be attached to it and be positioned rigidly using attachment points
This module interacts with the Bump physics module via messages and class public members
and with attached vessels via messages.

Written by Abhijit Nandy
License : Apache License 2.0

*/

#define STRICT
#define ORBITER_MODULE

#include "orbitersdk.h"
#include "BulletBase.h"

//Useful global functions
char output[1000];

/**
 * The constructor, does nothing currently
 */
BulletBase::BulletBase (OBJHANDLE hVessel, int flightmodel)
		: VESSEL3 (hVessel, flightmodel)	
{}


/**
 * Destructor, does nothing currently
 *
 */
BulletBase::~BulletBase()
{
	oapiWriteLog("BulletBase::~BulletBase() : called");
}



/**
 * Sets the capabilities of the BulletBase.
 * TODO: may need to do more than just set vessel names, eg read a config file
 * and load its static mesh, setup LOD etc.
 */
void BulletBase::clbkSetClassCaps (FILEHANDLE cfg)
{
	
	
	// Physical Specs : TODO ground the base completely
	SetSize (1.2);
	SetClipRadius(0.5); // <---adjust, also camera viewpoint, translate man down etc	
	SetPMI (_V(0.61,0.01,0.62));
	SetEmptyMass(100);
	SetTouchdownPoints( _V(-0.1, 0, -0.07), _V(0.1, 0, -0.07), _V(0.0, 0, 0.170) ); //this is imp to keep vessels on ground
	
	baseCmd = 0;	
	oapiWriteLog("BulletBase: classcaps called ");	

}


/**
 * Receives 2 types of messages:
 * id 1 : Transforms from Bump module for VESSELs registered by this base with Bump
 * id 2 : Commands from attached VESSELs to forward to Bump
 * 			(a direct channel to Bump for these VESSELs would be better to prevent
 * 			mingling with BulletBase commands)
 */
int	BulletBase::clbkGeneric( int  msgid, int  prm ,  void *  context)
{

	struct packet* pkt;
	
	switch(msgid){
		//Transforms from Bump module
		case 1:	
			switch(prm){
				//Engine force
				case 1:
					pkt = (struct packet*)context;					
					break;
				//Breaking force
				case 2:
					break;

			}		
			break;

		//Keys from an attached vessel
		case 2:
			baseCmd = prm;
			//sprintf(oapiDebugString(),"BulletBase : got baseCmd from bbv %d", baseCmd);
			break;

		default:
			sprintf(oapiDebugString(),"BulletBase : Unknown message to clbkGeneric %d", msgid);
	}	
	return 0;
}


/**
 * Finds and attaches VESSELs in the list in first call.
 */
void BulletBase::clbkPreStep(double SimT, double SimDT, double MJD) 
{
	

	if(baseCmd == OAPI_KEY_SPACE)
		baseCmd = NO_KEY;

	
}


/**
 * Reads keys for base specific commands.
 */
int BulletBase::clbkConsumeBufferedKey(DWORD key, bool down, char *kstate) {
	
	/*sprintf(output, "clbkConsumeBufferedKey : %d", baseCmd);
	oapiWriteLog(output);*/
	
	if (down){	//key is pressed
		if (KEYMOD_SHIFT (kstate)) { //Shift key has also been pressed

		} 
		else { // unmodified keys
			baseCmd = key;
			//sprintf(oapiDebugString(),"baseCmd..%d", baseCmd);
		}
	}
	else{	//key was released
		if (KEYMOD_SHIFT (kstate)) {

		} 
		else { // unmodified keys
			baseCmd = NO_KEY;
		}
	}

	return 0;
}

/**
 * Save any base specific state, currently used to avoid saving a base file.
 */
void BulletBase::clbkSaveState(FILEHANDLE  scn)
{
	//may be used later
}


/**
 * Vessel initialization
 */
DLLCLBK VESSEL *ovcInit (OBJHANDLE hvessel, int flightmodel)
{
	return new BulletBase (hvessel, flightmodel);
}


/**
 * Vessel cleanup
 * TODO: Ensure cleanup of all registered physic objects with Bump
 */
DLLCLBK void ovcExit (VESSEL *vessel)
{

	oapiWriteLog("BulletBase: Exiting");

	if (vessel) delete (BulletBase*)vessel;
}

